package com.hqyj.springbootdemo01.shiro;

import com.alibaba.fastjson.JSONObject;
import com.hqyj.springbootdemo01.common.component.JwtComponent;
import com.hqyj.springbootdemo01.common.config.JWTToken;
import com.hqyj.springbootdemo01.user.dto.UserRole;
import com.hqyj.springbootdemo01.user.entity.User;
import com.hqyj.springbootdemo01.user.service.UserService;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
 * @author sdx2009
 * @package com.hqyj.springbootdemo01.shiro
 * @date 2022/8/30 9:12
 */
public class UserRealm extends AuthorizingRealm {

    @Autowired
    private UserService userService;
    @Autowired
    private JwtComponent jwtComponent;

    /**
     * 授权
     *
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {

//        获取用户信息
        JSONObject userJson = JSONObject.parseObject(principalCollection.toString());
        Integer id = userJson.getInteger("id");
//      查询数据库，根据用户id获取用户角色、权限等信息
        UserRole userRole = userService.getUserRoleByUserId(id);

//        获取到userRole对象后，把roleName传给shiro的SimpleAuthorizationInfo对象
        Set<String> roleSet = new HashSet<>();
        if (!Objects.isNull(userRole)) {
            roleSet.add(userRole.getRoleName());
        }

//        获取权限集合
        Set<String> permSet = new HashSet<>();

//      把roleSet添加到SimpleAuthorizationInfo对象中
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.addRoles(roleSet);

//        把用户权限集合添加到SimpleAuthorizationInfo对象中
        authorizationInfo.setStringPermissions(permSet);
        return authorizationInfo;
    }

    /**
     * 认证
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

//        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;

//        获取并处理JWT token
        String token = authenticationToken.getPrincipal().toString();
        String userName = jwtComponent.getUserNameByToken(token);

        User user = userService.getUserInfoByName(userName);

        if (Objects.isNull(user)) {
            throw new ShiroException();
        }
//        这里传token做身份验证
        SimpleAuthenticationInfo simpleAuthenticationInfo =
                new SimpleAuthenticationInfo(JSONObject.toJSONString(user),
                        token, this.getName());

        return simpleAuthenticationInfo;
    }

    /**
     * 支持JWTToken
     *
     * @param token
     * @return
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JWTToken;
    }
}
